#coding=utf8

import time
from urllib import parse,request

class WX(object):
    def __init__(self):
        self.MemberList = {} #好友列表

    def getUUID():
        url = 'https://login.weixin.qq.com/jslogin'
        params = {
            'appid': 'wx782c26e4c19acffb',
            'fun': 'new',
            'lang': 'zh_CN',
            '_': int(time.time()),}
        res = self.request(url,params)
        # window.QRLogin.code = 200; window.QRLogin.uuid = "oZwt_bFfRg==";
        regx = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"'
        pm = re.search(regx, data)
        code = pm.group(1)
        uuid = pm.group(2)
        if code == '200':
            return True
        return False


    def login(self):



    def webwxgetcontact():
        """获取好友列表"""
        url = base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % (pass_ticket, skey, int(time.time()))

        request = urllib2.Request(url = url)
        request.add_header('ContentType', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        data = response.read()


        dic = json.loads(data)
        MemberList = dic['MemberList']

        #特殊帐号 
        SpecialUsers = ['newsapp', 
                        'fmessage', 
                        'filehelper',
                        'weibo', 
                        'qqmail', 
                        'fmessage', 
                        'tmessage', 
                        'qmessage', 
                        'qqsync', 
                        'floatbottle', 
                        'lbsapp', 
                        'shakeapp', 
                        'medianote', 
                        'qqfriend',
                        'readerapp', 
                        'blogapp', 
                        'facebookapp',
                        'masssendapp',
                        'meishiapp', 
                        'feedsapp', 
                        'voip', 
                        'blogappweixin',
                        'weixin',
                        'brandsessionholder',
                        'weixinreminder', 
                        'wxid_novlwrv3lqwv11', 
                        'gh_22b87fa7cb3c', 
                        'officialaccounts',
                        'notification_messages',
                        'wxid_novlwrv3lqwv11',
                        'gh_22b87fa7cb3c',
                        'wxitil',
                        'userexperience_alarm', 
                        'notification_messages']
        for i in range(len(MemberList) - 1, -1, -1):
            Member = MemberList[i]
            if Member['VerifyFlag'] & 8 != 0: # 公众号/服务号
                MemberList.remove(Member)
            elif Member['UserName'] in SpecialUsers: # 特殊账号
                MemberList.remove(Member)
            elif Member['UserName'].find('@@') != -1: # 群聊
                MemberList.remove(Member)
            elif Member['UserName'] == My['UserName']: # 自己
                MemberList.remove(Member)

        return MemberList

    def createChatroom(UserNames):
        """创建群聊"""
        MemberList = []
        for UserName in UserNames:
            MemberList.append({'UserName': UserName})


        url = base_uri + '/webwxcreatechatroom?pass_ticket=%s&r=%s' % (pass_ticket, int(time.time()))
        params = {
            'BaseRequest': BaseRequest,
            'MemberCount': len(MemberList),
            'MemberList': MemberList,
            'Topic': '',
        }

        request = urllib2.Request(url = url, data = json.dumps(params))
        request.add_header('ContentType', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        data = response.read()

        # print data

        dic = json.loads(data)
        ChatRoomName = dic['ChatRoomName']
        MemberList = dic['MemberList']
        DeletedList = []
        for Member in MemberList:
            if Member['MemberStatus'] == 4: #被对方删除了
                DeletedList.append(Member['UserName'])

        ErrMsg = dic['BaseResponse']['ErrMsg']
        # if len(ErrMsg) > 0:
        #   print ErrMsg

        return ChatRoomName, DeletedList

    def deleteMember(ChatRoomName, UserNames):
        """从群聊删除成员"""
        url = base_uri + '/webwxupdatechatroom?fun=delmember&pass_ticket=%s' % (pass_ticket)
        params = {
            'BaseRequest': BaseRequest,
            'ChatRoomName': ChatRoomName,
            'DelMemberList': ','.join(UserNames),
        }

        request = urllib2.Request(url = url, data = json.dumps(params))
        request.add_header('ContentType', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        data = response.read()

        # print data

        dic = json.loads(data)
        ErrMsg = dic['BaseResponse']['ErrMsg']
        # if len(ErrMsg) > 0:
        #   print ErrMsg

        Ret = dic['BaseResponse']['Ret']
        if Ret != 0:
            return False
            
        return True

    def addMember(ChatRoomName, UserNames):
        """群聊增加成员"""
        url = base_uri + '/webwxupdatechatroom?fun=addmember&pass_ticket=%s' % (pass_ticket)
        params = {
            'BaseRequest': BaseRequest,
            'ChatRoomName': ChatRoomName,
            'AddMemberList': ','.join(UserNames),
        }

        request = urllib2.Request(url = url, data = json.dumps(params))
        request.add_header('ContentType', 'application/json; charset=UTF-8')
        response = urllib2.urlopen(request)
        data = response.read()

        # print data

        dic = json.loads(data)
        MemberList = dic['MemberList']
        DeletedList = []
        for Member in MemberList:
            if Member['MemberStatus'] == 4: #被对方删除了
                DeletedList.append(Member['UserName'])

        ErrMsg = dic['BaseResponse']['ErrMsg']
        # if len(ErrMsg) > 0:
        #   print ErrMsg

        return DeletedList